]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
lookingGlass: Evaluate command asynchronously
authorFlorian Müllner <fmuellner@gnome.org>
Wed, 12 Jul 2023 19:40:05 +0000 (21:40 +0200)
committerMarge Bot <marge-bot@gnome.org>
Fri, 14 Jul 2023 12:36:53 +0000 (12:36 +0000)
This allows using await in the command (or the header we
add to it), for example when handling Promises or importing
a module dynamically.

The latter will be crucial when porting to ESM.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2842>

js/ui/lookingGlass.js

index ac618200c8fff1d5ae3f6292a7793355c4accc1a..7feb103918404178693783864b184178f32d3618 100644 (file)
@@ -28,7 +28,7 @@ const CHEVRON = '>>> ';
 /* Imports...feel free to add here as needed */
 const commandHeader = `
     const {Clutter, Gio, GLib, GObject, Meta, Shell, St} = imports.gi;
-    const Main = imports.ui.main;
+    const Main = await imports.ui.main;
 
     /* Utility functions...we should probably be able to use these
      * in the shell core code too. */
@@ -39,6 +39,7 @@ const commandHeader = `
     const it = Main.lookingGlass.getIt();
     const r = Main.lookingGlass.getResult.bind(Main.lookingGlass);
     `;
+const AsyncFunction = async function () {}.constructor;
 
 const HISTORY_KEY = 'looking-glass-history';
 // Time between tabs for them to count as a double-tab event
@@ -1412,7 +1413,7 @@ class LookingGlass extends St.BoxLayout {
             // Ensure we don't get newlines in the command; the history file is
             // newline-separated.
             text = text.replace('\n', ' ');
-            this._evaluate(text);
+            this._evaluate(text).catch(logError);
             return true;
         });
 
@@ -1528,7 +1529,7 @@ class LookingGlass extends St.BoxLayout {
         }
     }
 
-    _evaluate(command) {
+    async _evaluate(command) {
         command = this._history.addItem(command); // trims command
         if (!command)
             return;
@@ -1540,7 +1541,7 @@ class LookingGlass extends St.BoxLayout {
 
         let resultObj;
         try {
-            resultObj = Function(fullCmd)();
+            resultObj = await AsyncFunction(fullCmd)();
         } catch (e) {
             resultObj = `<exception ${e}>`;
         }